<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="jianzhu" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartmentsFn(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartmentsFn(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepartmentsFn(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="isEdit ? '编辑':'添加'" :visible.sync="dialogTableVisible">
        <AddorEdit v-if="dialogTableVisible" :existed-code-list="existedCodeList" :existed-name-list="cExistedName" :pid="curId" :is-edit="isEdit" @updata="close" @onClose="isClose" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTree } from '@/utils/index'
import AddorEdit from './AddorEdit.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      list: [],
      existedCodeList: [], // 表单校验拿到code 判断不能重复
      originList: [],
      dialogTableVisible: false,
      peoples: [],
      curId: '',
      isEdit: false
    }
  },
  computed: {
    // 统计部门名称
    cExistedName() {
      // 点击时不同按钮式 要的 pid的值不同
      // 点击添加要的是子级的pid
      // 点击编辑时要的是同级的pid
      // 所有又要把id去理一下
      let curpId = this.curId
      if (this.isEdit) {
        const res = this.originList.find(item => item.id === this.curId)
        curpId = res.pid
      }
      return this.originList.filter(item => item.pid === curpId).map(item => item.name)
    }
  },
  created() {
    this.getDepartmentsFn()
  },
  methods: {
    async getDepartmentsFn() {
      const res = await getDepartments()
      console.log('我是所有', res)
      this.list = tranListToTree(res.data.depts)
      // 遍历筛选出code的数据 然后传给子组件
      this.existedCodeList = res.data.depts.map(item => item.code)
      // 先创建一个数组 来接受我想要的数据 在通过计算属性监听我的数组变化
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          name: item.name,
          pid: item.pid
        }
      })
      console.log(this.originList)
    },
    // 删除
    delDepartmentsFn(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        console.log('aa')
        // 删除操作成功
        await delDepartments(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.getDepartmentsFn()
      }).catch(error => {
        console.log(error)
      })
    },
    // 添加
    addDepartmentsFn(id) {
      this.dialogTableVisible = true
      this.curId = id
      this.isEdit = false
    },
    close() {
      this.dialogTableVisible = false
      this.getDepartmentsFn()
    },
    // 编辑
    editDepartmentsFn(id) {
      this.dialogTableVisible = true
      this.curId = id
      this.isEdit = true
    },
    isClose() {
      this.dialogTableVisible = false
    }
  }
}
</script>

<style>

</style>
